﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-11 17:55:09
 * 功能：接收由AF SDK 调用的回调函数
**************************************************************************************/

using AppsFlyerSDK;
using Game.Framework;
using System.Collections.Generic;
using UnityEngine;

public class AFCallbackReceiver : MonoBehaviour, IAppsFlyerConversionData
{
    private void Start()
    {
        LogManager.Log($"AppsFlyerManager已运行");
        AppsFlyer.initSDK("DjnAvdKWWSnqJqU5vxVqSC", "6751632404", this);
        //LogManager.LogError("发布前请替换SDK的devKey和appID", "重要提醒");

#if UNITY_IOS && !UNITY_EDITOR
        AppsFlyer.waitForATTUserAuthorizationWithTimeoutInterval(60);
#endif

        AppsFlyer.startSDK();
        LogManager.Log($"初始化AF");
    }

    public void onAppOpenAttribution(string attributionData)
    {
        LogManager.Log($"AppsFlyerManager启动 onAppOpenAttribution: This method was replaced by UDL. This is a fake call. data:{attributionData}", "AF");
    }

    public void onAppOpenAttributionFailure(string error)
    {
        LogManager.Log($"AppsFlyerManager启动 onAppOpenAttributionFailure: This method was replaced by UDL. This is a fake call. error:{error}", "AF");
    }

    public void onConversionDataFail(string error)
    {
        LogManager.Log("AppsFlyerManager启动 onconversiondatafail" + error, "AF");
    }

    public void onConversionDataSuccess(string conversionData)
    {
        LogManager.Log($"AppsFlyerManager启动 onConversionDataSuccess，{conversionData}", "AF");
        Dictionary<string, object> conversionDataDictionary = AppsFlyer.CallbackStringToDictionary(conversionData);

        // 处理转化数据
        if (conversionDataDictionary != null)
        {
            if (conversionDataDictionary.TryGetValue("media_source", out object source))
            {
                LogManager.Log($"AppsFlyerManager启动 Media Source: {source}", "AF");
                // Const.setSourceStatic(source.ToString());
            }

            // 是否是首次安装
            if (conversionDataDictionary.TryGetValue("is_first_launch", out object isFirstLaunch))
            {
                LogManager.Log($"AppsFlyerManager启动 Is First Launch: {isFirstLaunch}", "AF");
            }
        }
    }
}